
// 重写fetch
const script = document.createElement('script')
script.setAttribute('type', 'text/javascript')
script.setAttribute('src', chrome.runtime.getURL('pageScripts/main.js'))
document.documentElement.appendChild(script)
script.addEventListener('load', () => {
  console.log('插件初始化成功')
})


// function testjj() {
//   console.log('触发testjj')
//   chrome.runtime.sendMessage({ type: 'message', text: "从content_script发出的信息"}, function(response) {
//     console.log(response);
//   });
//   setTimeout(() => {
//     testjj()
//   },1000 * 6)
// }

// // 发送消息到 devtools 新面板
// chrome.runtime.sendMessage({type: "message", text: "Hello from content script!"});

// // 监听来自 devtools 新面板的消息
// chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
//   if (message.type === "reply") {
//     console.log("Received reply: " + message.text);
//   }
// });

//const port = chrome.runtime.connect({ name: 'content_script' });
window.addEventListener('ssePostViewer', (event) => {
  console.log("hcsy85784>>>>", event.detail)
  try {
    chrome.runtime.sendMessage({ type: 'sse', text: event.detail }, function(...args) {
      console.log(...args)
    })
  } catch (error) {
    console.error(error)
  }
  // port.postMessage({ message: event.detail });
})
// port.onMessage.addListener((msg) => {
//   console.log('Received message from devtools:', msg);
// });


chrome.runtime.onMessage.addListener(
  function (request, _sender, _sendResponse) {
    console.log("request>>>>", request)
    console.log("_sender", _sender)
    console.log("_sendResponse", _sendResponse)
    console.log("request.url", request.url)
    const es = new EventSource(request.url)
    es.onerror = function (_ev) {
      es.close()
    }
  },
)
